/**
 * Copyright (c) 2012, David Varbel
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 * 
 * 1) Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.epistrata.experiment;

import java.util.HashMap;
import java.util.List;

public class Run {
	private long startTime = 0;
	private long finishTime = 0;
	private HashMap<String, String> factors;
	private final int number;
	private final long seed;
	private HashMap<String, String> responses = new HashMap<String, String>();
	private Experiment experiment;
	
	@SuppressWarnings("unchecked")
	public Run(Experiment experiment, int runNumber, long seed, HashMap<String, String> factors, List<String> responseTypes) {
		if (experiment == null) {
			throw new IllegalArgumentException("An experiment must be provided.");
		}
		if (factors == null) {
			throw new IllegalArgumentException("Factors must be provided.");
		}
		this.experiment = experiment;
		this.number = runNumber;
		this.factors = (HashMap<String, String>) factors.clone();
		for (String responseType : responseTypes) {
			this.responses.put(responseType, "");
		}
		this.seed = seed;
	}
	

	/**
	 * Package private method to note the start time of the run.
	 */
	void setStartTime() {
		startTime = System.nanoTime();
	}

	/**
	 * Package private method to note the finish time of the run.
	 */
	void setFinishTime() {
		finishTime = System.nanoTime();
	}
	
	public int getRunNumber() {
		return number;
	}


	public void message(String msg) {
		experiment.message(this, msg);
	}


	@SuppressWarnings("unchecked")
	public HashMap<String, String> getFactors() {
		return (HashMap<String, String>) factors.clone();
	}
	
	public long getRandomSeed() {
		return seed;
	}
	
	public void setResponse(String type, String response) {
		if (!responses.containsKey(type)) {
			throw new IllegalArgumentException("Attempted to set a response of an undefined type: " + type);
		}
		responses.put(type, response);
	}
	
	@SuppressWarnings("unchecked")
	public HashMap<String, String> getResponses() {
		return (HashMap<String, String>) responses.clone();
	}
	
	public long getRunDuration() {
		if (startTime == 0 || finishTime == 0) {
			throw new IllegalStateException("Cannot get run duration until after run has finished.");
		}
		return finishTime - startTime;
	}
	
	public void dataUpdate(Model model) {
		experiment.sendDataUpdate(this, model);
	}
}
